import { createRouter, createWebHistory } from 'vue-router'; // createWebHashHistory, createWebHistory
import type { Router, RouteRecordRaw, RouteComponent } from 'vue-router';

const Layout = ():RouteComponent => import('@/layout/index.vue');

import tableRouter from './modules/table';

export const constantRoutes:RouteRecordRaw[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/register',
    component: () => import('@/views/register/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect.vue'),
    meta: { hidden: true }
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    meta: { hidden: true }
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue'),
    meta: { hidden: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    meta: { hidden: true },
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index.vue'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  }
];

export const asyncRoutes:RouteRecordRaw[] = [
  {
    path: '/zip',
    component: Layout,
    redirect: '/zip/download',
    name: '检索管理',
    meta: { alwaysShow: true, title: '检索管理', icon: 'zip' },
    children: [
      {
        path: 'textSearch',
        component: () => import('@/views/zip/text-search.vue'),
        name: 'TextSearch',
        meta: { title: '文本检索' }
      },
      {
        path: 'imageSearch',
        component: () => import('@/views/zip/image-search.vue'),
        name: 'ImageSearch',
        meta: { title: '图像检索' }
      },
      {
        path: 'uploadImage',
        component: () => import('@/views/zip/upload-image.vue'),
        name: 'ExportZip',
        meta: { title: '上传图像' }
      },
      {
        path: 'databases',
        component: () => import('@/views/zip/databases.vue'),
        name: 'Databases',
        meta: { title: '数据库管理' }
      }
    ]
  },
  tableRouter,

  { path: '/:pathMatch(.*)*', redirect: '/404', meta: { hidden: true }}
];

console.log('BASE_URL=', import.meta.env);

const createTheRouter = ():Router => createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior: () => ({ top: 0 }),
  routes: constantRoutes
});

interface RouterPro extends Router {
  matcher: unknown;
}

const router = createTheRouter() as RouterPro;

export function resetRouter() {
  const newRouter = createTheRouter() as RouterPro;
  router.matcher = newRouter.matcher;
}

export default router;
